<?php

declare(strict_types=1);

namespace App\CustHub\Controller;

use App\Common\Service\QwNotifyService;
use App\CustHub\Interfaces\OpenWorkApplicationInterface;
use App\CustHub\Service\LicenseService;
use App\CustHub\Work\OpenWorkAgentService;
use App\CustHub\Work\TxlAgentService;
use EasyWeChat\OpenWork\Message;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Mine\MineController;
use Psr\SimpleCache\CacheInterface;

#[Controller(prefix: 'callback')]
class OpenWorkCallbackController extends MineController
{
    #[Inject]
    protected OpenWorkApplicationInterface $workApplication;

    #[Inject]
    protected CacheInterface $cache;

    #[Inject]
    protected QwNotifyService $wkyService;

    #[Inject]
    protected LicenseService $licenseService;

    #[RequestMapping('systemCallback')]
    public function systemCallback()
    {
        $app = $this->workApplication->getOpenWork();
        try {
            $server = $app->getServer();
            $server->with(function($message, \Closure $next) {
                $InfoType = $message->InfoType;
                if($InfoType == 'license_pay_success'){
                    //接口许可证余额购买成功
                    $this->licenseService->paySuccess($message->OrderId,$message->AuthCorpId);
                }
                return $next($message);
            });

            return $server->serve();
        } catch (\Throwable $e) {
            logger()->info('代开发SuiteTicket刷新失败'.$e->getMessage());
        }
    }

    #[RequestMapping('txlCallback')]
    public function txlCallback()
    {
        $app = $this->workApplication->getTxlWork();
        try {
            $server = $app->getServer();

            $server->handleSuiteTicketRefreshed(function(Message $message, \Closure $next) use ($app) {
                $suiteId = $message->SuiteId;
                $suiteTicket = $message->SuiteTicket;
                $verifyTicketKey = 'txlSuiteTicket:'.$suiteId;
                $this->cache->set($verifyTicketKey, $suiteTicket,30 * 60);
                return $next($message);
            });

            $server->handleAuthCreated(function(Message $message, \Closure $next) use ($app) {
                //授权成功通知
                container()->get(TxlAgentService::class)->saveAuth($message);
                return $next($message);
            });
            return $server->serve();
        } catch (\Throwable $e) {
            logger()->info('代开发SuiteTicket刷新失败'.$e->getMessage());
        }
    }


    #[RequestMapping('agentOpenCallback')]
    public function agentOpenCallback()
    {
        $app = $this->workApplication->getOpenWork();
        try {
            $server = $app->getServer();
            $server->handleSuiteTicketRefreshed(function(Message $message, \Closure $next) use ($app) {
                $suiteId = $message->SuiteId;
                $suiteTicket = $message->SuiteTicket;
                $verifyTicketKey = 'openWorkSuiteTicket:'.$suiteId;
                $this->cache->set($verifyTicketKey, $suiteTicket,30 * 60);
                return $next($message);
            });
            $server->handleAuthCreated(function(Message $message, \Closure $next) use ($app) {
                //授权成功通知
                container()->get(OpenWorkAgentService::class)->saveWorkAgent($message);
                return $next($message);
            });

            $server->handleAuthChanged(function(Message $message, \Closure $next) use ($app) {
                //变更授权通知
                container()->get(OpenWorkAgentService::class)->changeAuth($message);
                return $next($message);
            });


            $server->handleAuthCancelled(function(Message $message, \Closure $next) use ($app) {
                //取消授权通知
                container()->get(OpenWorkAgentService::class)->delWorkAgent($message->SuiteId,$message->AuthCorpId);
                return $next($message);
            });
            $server->handleResetPermanentCode(function(Message $message, \Closure $next) use ($app) {
                //重置永久授权码通知
                container()->get(OpenWorkAgentService::class)->saveWorkAgent($message);
                return $next($message);
            });

            $server->with(function($message, \Closure $next) {
                stdout_log()->info('接收到消息：'.json_encode($message->toArray()));
                $this->wkyService->pushQwWorkMessage($message->toArray());
                return $next($message);
            });
            return $server->serve();
        } catch (\Throwable $e) {
            logger()->info('代开发SuiteTicket刷新失败'.$e->getMessage());
        }
    }
}